home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume24 / watch < prev    next >
Encoding:
Internet Message Format  |  1991-06-05  |  7.5 KB

  1. Subject:  v24i084:  Repeatedly execute a program under curses(3)
  2. Newsgroups: comp.sources.unix
  3. Approved: rsalz@uunet.UU.NET
  4. X-Checksum-Snefru: 7804f045 1dfb7334 fc173470 b947b7db
  5.  
  6. Submitted-by: Tony Rems <rembo@unisoft.com>
  7. Posting-number: Volume 24, Issue 84
  8. Archive-name: watch
  9.  
  10. [  I had to comment out EXTHDRS, the header dependencies for watch.o,
  11.    and remove $(LIBS) as a dependent in $(DEST)/$(PROGRAM); you might have
  12.    to do some fiddling, too.  It's nice to have a version of this tool
  13.    that is not covered by obnoxious copyrights.  --r$ ]
  14.  
  15. The following program is called 'watch' it is a curses(3) based program
  16. that allows you to specify a command such as ps(1) to "watch".  It
  17. defaults to updating every two seconds and displays the time.  It's much
  18. nicer than trying to type things over every couple of seconds or using a
  19. for loop.
  20.  
  21. -------------------------cut-here------------------------------------
  22. #! /bin/sh
  23. # This is a shell archive, meaning:
  24. # 1. Remove everything above the #! /bin/sh line.
  25. # 2. Save the resulting text in a file.
  26. # 3. Execute the file with /bin/sh (not csh) to create:
  27. #    Makefile
  28. #    watch.c
  29. #    README
  30. #    MANIFEST
  31. #    watch.1
  32. # This archive created: Thu Mar  7 21:57:47 1991
  33. export PATH; PATH=/bin:/usr/bin:$PATH
  34. if test -f 'Makefile'
  35. then
  36.     echo shar: "will not over-write existing file 'Makefile'"
  37. else
  38. cat << \SHAR_EOF > 'Makefile'
  39. DEST          = .
  40.  
  41. CC          = cc
  42.  
  43. #uncomment the following line if you are using a System V machine
  44. #CFLAGS          = -DATT 
  45.  
  46. #EXTHDRS          = /usr/include/curses.h \
  47. #        /usr/include/sgtty.h \
  48. #        /usr/include/signal.h \
  49. #        /usr/include/stdio.h \
  50. #        /usr/include/sys/fcntl.h \
  51. #        /usr/include/sys/ioctl.h \
  52. #        /usr/include/sys/ioctl.h \
  53. #        /usr/include/sys/sgtty.h \
  54. #        /usr/include/sys/sysmacros.h \
  55. #        /usr/include/sys/sysmacros.h \
  56. #        /usr/include/sys/ttychars.h \
  57. #        /usr/include/sys/ttychars.h \
  58. #        /usr/include/sys/ttydev.h \
  59. #        /usr/include/sys/ttydev.h \
  60. #        /usr/include/sys/types.h \
  61. #        /usr/include/sys/types.h
  62.  
  63. HDRS          =
  64.  
  65. LDFLAGS          =
  66.  
  67. LIBS          = -lcurses -ltermcap
  68.  
  69. LINKER          = cc
  70.  
  71. MAKEFILE      = Makefile
  72.  
  73. FILES          = Makefile watch.c README MANIFEST watch.1
  74.  
  75. OBJS          = watch.o
  76.  
  77. PRINT          = pr
  78.  
  79. PROGRAM          = watch
  80.  
  81. SRCS          = watch.c
  82.  
  83. all:        $(PROGRAM)
  84.  
  85. $(PROGRAM):     $(OBJS) 
  86.         @echo -n "Loading $(PROGRAM) ... "
  87.         @$(LINKER) $(LDFLAGS) $(OBJS) -o $(PROGRAM) $(LIBS)
  88.         @echo "done"
  89.  
  90. clean:;        @echo "rm -f $(OBJS)"
  91.         @rm -f $(OBJS) 
  92.  
  93. depend:;    @mkmf -f $(MAKEFILE) PROGRAM=$(PROGRAM) DEST=$(DEST)
  94.  
  95. index:;        @ctags -wx $(HDRS) $(SRCS)
  96.  
  97. install:    $(PROGRAM)
  98.         @echo Installing $(PROGRAM) in $(DEST)
  99.         @install -s $(PROGRAM) $(DEST)
  100.  
  101. shar:;        @echo "shar $(FILES) > $(PROGRAM).shar
  102.         @shar $(FILES) > $(PROGRAM).shar
  103.  
  104. print:;        @$(PRINT) $(HDRS) $(SRCS)
  105.  
  106. program:        $(PROGRAM)
  107.  
  108. tags:           $(HDRS) $(SRCS); @ctags $(HDRS) $(SRCS)
  109.  
  110. update:        $(DEST)/$(PROGRAM)
  111.  
  112. $(DEST)/$(PROGRAM): $(SRCS) $(HDRS) $(EXTHDRS)
  113.         @make -f $(MAKEFILE) DEST=$(DEST) install
  114. ###
  115. #watch.o: /usr/include/curses.h /usr/include/stdio.h /usr/include/sgtty.h \
  116. #    /usr/include/sys/ioctl.h /usr/include/sys/ttychars.h \
  117. #    /usr/include/sys/ttydev.h /usr/include/sys/ttychars.h \
  118. #    /usr/include/sys/ttydev.h /usr/include/sys/sgtty.h \
  119. #    /usr/include/sys/ioctl.h /usr/include/signal.h \
  120. #    /usr/include/sys/fcntl.h /usr/include/sys/types.h \
  121. #    /usr/include/sys/sysmacros.h /usr/include/sys/sysmacros.h \
  122. #    /usr/include/sys/types.h
  123. SHAR_EOF
  124. fi
  125. if test -f 'watch.c'
  126. then
  127.     echo shar: "will not over-write existing file 'watch.c'"
  128. else
  129. cat << \SHAR_EOF > 'watch.c'
  130. #include <curses.h>
  131. #include <stdio.h>
  132. #include <signal.h>
  133. #include <sys/fcntl.h>
  134.  
  135. #ifdef ATT
  136. #define    crmode()    cbreak()
  137. #define bzero(s,n)    memset(s,0,n)
  138. #endif
  139.  
  140. void    die();
  141. extern    FILE         *popen();
  142. extern    int         pclose();
  143. extern    long        time();
  144. extern    char        *ctime();
  145.  
  146. main(argc, argv)
  147. int argc;
  148. char *argv[];
  149. {
  150.     int hor = 1, ver = 0;
  151.     FILE *piper;
  152.     char buf[180];
  153.     char cmd[128];
  154.     int count = 1;
  155.     long timer;
  156.     int nsecs = 2;
  157.  
  158.     if (argc < 2) {
  159.         fprintf(stderr, "Usage: %s command [args]\n", argv[0]);
  160.         exit(1);
  161.     } /* if */
  162.  
  163.     /* If -n is specified, convert the next argument to the numver
  164.      * for the number of seconds
  165.      */
  166.     if (strcmp(argv[1], "-n") == 0) {
  167.         nsecs = atoi(argv[2]);
  168.         count = 3;
  169.         if (nsecs == 0 || argc < 3) {
  170.             fprintf(stderr, "Usage: %s command [args]\n", argv[0]);
  171.             exit(1);
  172.         } /* if */
  173.     } /* if */
  174.  
  175.     /* Build command string to give to popen */
  176.     (void)bzero(cmd, sizeof(cmd));
  177.     strcpy(cmd, argv[count]);
  178.     while (++count < argc) {
  179.         strcat(cmd, " ");
  180.         strcat(cmd, argv[count]);
  181.     } /* while */
  182.  
  183.     /* Catch keyboard interrupts so we can
  184.      * put tty back in a sane state 
  185.      */
  186.     (void) signal(SIGINT, die);
  187.     (void) signal(SIGTERM, die);
  188.     (void) signal(SIGHUP, die);
  189.  
  190.     /* Set up tty for curses use */
  191.     initscr();
  192.     nonl();
  193.     noecho();
  194.     crmode();
  195.  
  196.     while(1) { /* loop forever */
  197.  
  198.         /* Put up time interval and current time */
  199.         move(hor, ver);
  200.         time(&timer);
  201.         printw("Every %d seconds\t\t%s\t\t%s", nsecs, cmd, ctime(&timer));
  202.         hor = 3;
  203.  
  204.         /* Open pipe to command */
  205.         if ((piper = popen(cmd, "r")) == (FILE *)NULL) {
  206.             perror("popen");
  207.             exit(2);
  208.         } /* if */
  209.  
  210.  
  211.         /* Read in output from the command and make sure 
  212.          * that it will fit on 1 screen 
  213.          */
  214.         while ((fgets(buf, sizeof(buf), piper) != NULL) && hor < LINES) {
  215.             buf[COLS-1] = '\0';
  216.             mvaddstr(hor, ver, buf);
  217.             hor++;
  218.         } /* while */
  219.         refresh();
  220.  
  221.         sleep(nsecs);
  222.         hor = 1; /* Go back to the top of screen */
  223.         pclose(piper);
  224.  
  225.     } /* while */
  226. } /* main */
  227.  
  228.  
  229. void
  230. die()
  231. {
  232.     /* Ignore interrupts while we clear the screen
  233.      * and reset the tty 
  234.      */
  235.     (void) signal(SIGINT, SIG_IGN);
  236.     clear(); 
  237.     refresh(); 
  238.     endwin();
  239.     exit(0);
  240. } /* die */
  241. SHAR_EOF
  242. fi
  243. if test -f 'README'
  244. then
  245.     echo shar: "will not over-write existing file 'README'"
  246. else
  247. cat << \SHAR_EOF > 'README'
  248.  
  249. THE PROGRAM:
  250. watch is a program to catch and repeatedly update the output 
  251. from any program you want to repeatedly catch the output from.
  252. It is a curses based program and allows you to specify the 
  253. interval.
  254.  
  255. COMPILING IT:
  256. watch should compile with no problems on any 4.2 BSD or later
  257. system.  I've compiled it on a Pyramid and a Sun3 running 3.5
  258. and one running 4.0.3.  If you uncomment the CFLAGS line in the
  259. makefile, the program should compile on a System V3 or later 
  260. system.  I tested it out on the Pyramid in the AT&T universe
  261. and on an Intel 486 running SVR4.  If you still have problems,
  262. just send mail to rembo@unisoft.com.  I'll help if I can.
  263. SHAR_EOF
  264. fi
  265. if test -f 'MANIFEST'
  266. then
  267.     echo shar: "will not over-write existing file 'MANIFEST'"
  268. else
  269. cat << \SHAR_EOF > 'MANIFEST'
  270. You should have:
  271.     MANIFEST
  272.     README
  273.     Makefile
  274.     watch.c
  275.     watch.1
  276. SHAR_EOF
  277. fi
  278. if test -f 'watch.1'
  279. then
  280.     echo shar: "will not over-write existing file 'watch.1'"
  281. else
  282. cat << \SHAR_EOF > 'watch.1'
  283.  
  284.  
  285.  
  286. watch(1)         Pyramid OSx Operating System          watch(1)
  287.  
  288.  
  289.  
  290. NAME
  291.      watch - watch a program with update intervals
  292.  
  293. SYNOPSIS
  294.      watch  [-n] _s_e_c_o_n_d_s _p_r_o_g_r_a_m [ _a_r_g_s ... ]
  295.  
  296. DESCRIPTION
  297.      _w_a_t_c_h is a curses(3X) based program that allows 
  298.      you to watch a program as it changes.  By default, it 
  299.      updates itself every 2 seconds.  You can specify the number 
  300.      of seconds with the -n option.  The curses packages allows 
  301.      for quick updating of the screen through cursor 
  302.      optimization.  The program will end with a keyboard 
  303.      interrupt, which will leave the screen in a 
  304.      valid yet cleared state.
  305.  
  306. EXAMPLE
  307.      try:
  308.     
  309.      On BSD:
  310.     watch -n 1 ps u
  311.  
  312.      On System V:
  313.     watch -n 1 ps -f
  314.  
  315. SEE ALSO
  316.      curses(3X)
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329. Printed 11/8/90                                                 1
  330.  
  331.  
  332.  
  333. SHAR_EOF
  334. fi
  335. exit 0
  336. #    End of shell archive
  337.  
  338. exit 0 # Just in case...
  339.